<?php
    require_once($application_folder."/controllers/navigator.php");
    class Uredi_vesti extends navigator
    {
        function Uredi_vesti () 
        { 
            parent::navigator();
            if ($this->session->userdata('logged_in') != TRUE)
            {
                redirect(base_url().'login');
            }
            $this->load->model('vesti_m','vesti');
            $this->carabiner->js('jquery-ui-1.8.5.custom.min.js'); 
        }
        public function index($edit=FALSE,$id=NULL,$validate_form=NULL){             
            $this->data['strana'] = 'vesti';
            $this->data['podstrana'] = 'uredi_vest';
            $this->carabiner->js('vesti/jquery.validate-vest.js');
            $this->carabiner->js('vesti/uredi-vest.js');      
            $this->carabiner->js('vesti/ured-server-fajlove.js');
            $this->carabiner->js('file-upload/fileuploader.js');             
            $this->carabiner->css('fileuploader.css');

            $this->carabiner->js('tinymce/tiny_mce.js'); 

            $this->data['form'] = 'new';
            $this->data['validate'] = FALSE;
            if($edit==TRUE){
                $this->data['form'] = 'edit';
                $this->data['vest'] = $this->vesti->citaj_vest($id);
            }
            if($validate_form!=NULL){
                foreach ($_POST as $key => $value) {
                    $this->data[$key] = $value;
                }                
                $this->data['post'] = $_POST;
                $this->data['form'] = $validate_form; 
                $this->data['validate'] = TRUE; 

            }
            $this->navigate($this->data['strana'].'/'.$this->data['podstrana']);
        }
        function encode_json_get($html)
        {
            if(isset($_GET['jsoncall'])) {

                $html = preg_replace(
                array('/\n/','/\r/','/\t/'),
                array(''),
                $html);
                echo $_GET['jsoncall'] . '(' . json_encode(array('html'=>$html)) . ');';

            }else {
                echo json_encode(array('html',$html));
            }
        }
        function createavest(){            
            if($this->vesti->createavest()) {
                $_POST['info'] = true;
                $_POST['message'] = 'Vaša vijest je uspešno kreirana.';
                $this->lisaj_vesti();
            }else {
                //echo "NeÅ¡to nije uredu. Molimo pokuÅ¡ajte ponovo.";
                $this->index(false,NULL,'new');
            }
        }
        function editvest(){
            if($this->vesti->editvest()) {
                $_POST['info'] = true;
                $_POST['message'] = 'Vaša vijest je uspešno uređena.';
                $this->lisaj_vesti();
            }else {
                //echo "NeÅ¡to nije uredu. Molimo pokuÅ¡ajte ponovo.";
                $this->index(false,NULL,'edit');
            }
        }
        public function lisaj_vesti($info=NULL,$message=NULL){
            $this->data['strana'] = 'vesti';           
            $this->data['podstrana'] = 'home';              
            $this->data['vesti'] = $this->vesti->listaj_vesti();
            $this->carabiner->js('vesti/home.js');  
            $this->navigate($this->data['strana'].'/'.$this->data['podstrana']);
        }
        public function prikazi_vest(){
            $id = $_POST['id'];
            $this->data['vest'] = $this->vesti->citaj_vest($id);
            $html = $this->load->view('vesti/vest',$this->data,TRUE);
            echo json_encode(array('html'=>$html));
        }
        function vesti_status($id,$status){            
            if($this->vesti->vest_status($id,$status)) {
                echo json_encode(array('seccess'=>'seccess'));
            }else echo json_encode(array('seccess'=>'failed')); 
        }
        function anketa_prikaz($id){
            $this->data['strana'] = 'anketa';           
            $this->data['podstrana'] = 'prikaz';              
            $this->data['anketa_id'] = $id;              
            $this->navigate($this->data['strana'].'/'.$this->data['podstrana']);
        }

    }
?>
